package com.ruoyi.compensationManagement.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.compensationManagement.domain.PositionSettings;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.compensationManagement.mapper.CompensationSalarySettingMapper;
import com.ruoyi.compensationManagement.domain.CompensationSalarySetting;
import com.ruoyi.compensationManagement.service.ICompensationSalarySettingService;

/**
 * 薪级薪资设置Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-05
 */
@Service
public class CompensationSalarySettingServiceImpl implements ICompensationSalarySettingService 
{
    @Autowired
    private CompensationSalarySettingMapper compensationSalarySettingMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询薪级薪资设置
     * 
     * @param payId 薪级薪资设置主键
     * @return 薪级薪资设置
     */
    @Override
    public CompensationSalarySetting selectCompensationSalarySettingByPayId(Long payId)
    {
        return compensationSalarySettingMapper.selectCompensationSalarySettingByPayId(payId);
    }

    /**
     * 查询薪级薪资设置列表
     * 
     * @param compensationSalarySetting 薪级薪资设置
     * @return 薪级薪资设置
     */
    @Override
    public List<CompensationSalarySetting> selectCompensationSalarySettingList(CompensationSalarySetting compensationSalarySetting)
    {
        return compensationSalarySettingMapper.selectCompensationSalarySettingList(compensationSalarySetting);
    }

    /**
     * 新增薪级薪资设置
     * 
     * @param compensationSalarySetting 薪级薪资设置
     * @return 结果
     */
    @Override
    public int insertCompensationSalarySetting(CompensationSalarySetting compensationSalarySetting)
    {
        return compensationSalarySettingMapper.insertCompensationSalarySetting(compensationSalarySetting);
    }

    /**
     * 修改薪级薪资设置
     * 
     * @param compensationSalarySetting 薪级薪资设置
     * @return 结果
     */
    @Override
    public int updateCompensationSalarySetting(CompensationSalarySetting compensationSalarySetting)
    {
        //薪资类型
        if("T".equals(compensationSalarySetting.getTypeSelect())){
            compensationSalarySetting.setPayLevel(null);
            compensationSalarySetting.setPayWages(null);
        }else{
            //薪资等级
            compensationSalarySetting.setPayType(null);
        }
        return compensationSalarySettingMapper.updateCompensationSalarySetting(compensationSalarySetting);
    }

    /**
     * 批量删除薪级薪资设置
     * 
     * @param payIds 需要删除的薪级薪资设置主键
     * @return 结果
     */
    @Override
    public int deleteCompensationSalarySettingByPayIds(Long[] payIds)
    {
        //判断id是否被sys_user表引用
        for (Long payId : payIds) {
            //判断删除项是否有子类
            List<CompensationSalarySetting> compensationSalarySettingList = compensationSalarySettingMapper.selectChildNodeByPayId(payId);
            boolean flag = true;
            if(compensationSalarySettingList != null && compensationSalarySettingList.size() > 0){
                flag = false;
                for (CompensationSalarySetting compensationSalarySetting : compensationSalarySettingList) {
                    //判断薪级是否被引用
                    int count = sysUserMapper.selectUserCountByPayId(compensationSalarySetting.getPayId());
                    if(count > 0){
                        throw new GlobalException("薪级等级已被使用，无法删除薪级类型");
                    }
                }
            }
            //没有子节点说明是本身为叶子节点===>判断岗位级别是否被使用
            if(flag){
                int i = sysUserMapper.selectUserCountByPayId(payId);
                if(i > 0){
                    throw new GlobalException("薪级等级已被使用，无法删除");
                }
            }
        }
        return compensationSalarySettingMapper.deleteCompensationSalarySettingByPayIds(payIds);
    }

    /**
     * 删除薪级薪资设置信息
     * 
     * @param payId 薪级薪资设置主键
     * @return 结果
     */
    @Override
    public int deleteCompensationSalarySettingByPayId(Long payId)
    {
        return compensationSalarySettingMapper.deleteCompensationSalarySettingByPayId(payId);
    }

    @Override
    public List<CompensationSalarySetting> selectCompensationSalarySettingAndChildren() {
        List<CompensationSalarySetting> compensationSalarySettings = compensationSalarySettingMapper.selectCompensationSalarySettingList(null);
        //记录父节点
        List<CompensationSalarySetting> parent = compensationSalarySettings.stream()
                .filter(compensationSalarySetting -> "T".equals(compensationSalarySetting.getTypeSelect()))
                .collect(Collectors.toList());
        for (CompensationSalarySetting c1 : parent) {
            for (CompensationSalarySetting c2 : compensationSalarySettings) {
                if(c2.getParentId() != null && c2.getParentId().equals(c1.getPayId())){
                    c1.getChildren().add(c2);
                }
            }
        }
        return parent;
    }

    @Override
    public Long getUpPayScale(Long payId) {
        //根据当前payId查找父id
        CompensationSalarySetting compensationSalarySetting = compensationSalarySettingMapper.selectCompensationSalarySettingByPayId(payId);
        Long parentId = compensationSalarySetting.getParentId();
        Integer orderNum = compensationSalarySetting.getOrderNum();
        //获取升级后的payId
        Long upAfterPayId = compensationSalarySettingMapper.getUpPayScale(parentId,orderNum);
        if(upAfterPayId != null){
            return upAfterPayId;
        }
        return 0L;
    }

    @Override
    public CompensationSalarySetting selectCompensationSalarySettingByUserId(Long userId) {
        return compensationSalarySettingMapper.selectCompensationSalarySettingByUserId(userId);
    }
}